package com.example.fitnessservice.module.dao.usermapper;

import com.example.fitnessservice.module.pojo.DietFoodCategory;
import com.example.fitnessservice.module.pojo.SystemDietCookbook;
import org.aopalliance.intercept.Interceptor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface DietMapper {
    /**
     * 食物库功能
     * 根据食物分类查询食物
     * @return
     */
    @Select("select * from diet_food_category where food_cate = #{food_cate}")
    public List<DietFoodCategory> findFoodAll(String food_cate);

    /**
     * 食谱查询功能
     * 根据BMI查询食谱
     * @param bmi
     * @return
     */
    @Select("select * from system_diet_cookbook where bmi = #{bmi}")
    public List<SystemDietCookbook> findSystemCoobook(BigDecimal bmi);

    /**
     * 根据食物名称模糊查询食物
     * @param foodName
     * @return
     */
    @Select("select * from diet_food_category where food_name like '%${foodName}%'")
    public List<DietFoodCategory> findFoodByName(String foodName);

    /**
     * 当用户点击创建食谱时，创建一个带有用户id的空表
     * @param userId
     */
    @Insert("insert into user_diet_cookbook (user_id) values (#{userId})")
    public int createUserDietCookbookEmpty(int userId);

    /**
     * 向user_diet_cookbook_food表中添加早餐食物
     * @param userCookbookId
     * @param foodId
     * @return
     */
    @Insert("insert into user_diet_cookbook_food (user_cook_book_id, food_id, breakfast) values (#{userCookbookId}, #{foodId}, 1)")
    public int addBreakfastFood(int userCookbookId, int foodId);

    /**
     * 查找刚刚给用户创建的带有用户id但其余数据均为空的食谱的id
     * @return
     */
    @Select("SELECT id FROM user_diet_cookbook WHERE user_id IS NOT NULL AND breakfast_details IS NULL AND lunch_details IS NULL AND dinner_details IS NULL;")
    public int findEmptyDietCookbook();

    /**
     * 向user_diet_cookbook_food表中添加午餐食物
     * @param userCookbookId
     * @param foodId
     * @return
     */
    @Insert("insert into user_diet_cookbook_food (user_cook_book_id, food_id, lunch) values (#{userCookbookId}, #{foodId}, 1)")
    public int addLunchFood(int userCookbookId, int foodId);

    /**
     * 向user_diet_cookbook_food表中添加晚餐食物
     * @param userCookbookId
     * @param foodId
     * @return
     */
    @Insert("insert into user_diet_cookbook_food (user_cook_book_id, food_id, dinner) values (#{userCookbookId}, #{foodId}, 1)")
    public int addDinnerFood(int userCookbookId, int foodId);

    /**
     * 查找某一用户的某一食谱id下的所有早餐食物id
     * @return
     */
    @Select("select food_id from user_diet_cookbook_food where user_cook_book_id = #{cookbookId} and breakfast = 1")
    public List<Integer> findUserDietCookbookFoodBreakfast(Integer userId, Integer cookbookId);

    /**
     * 查找某一用户的某一食谱id下的所有午餐食物id
     * @return
     */
    @Select("select food_id from user_diet_cookbook_food where user_cook_book_id = #{cookbookId} and lunch = 1")
    public List<Integer> findUserDietCookbookFoodLunch(Integer userId, Integer cookbookId);

    /**
     * 查找某一用户的某一食谱id下的所有晚餐食物id
     * @return
     */
    @Select("select food_id from user_diet_cookbook_food where user_cook_book_id = #{cookbookId} and dinner = 1")
    public List<Integer> findUserDietCookbookFoodDinner(Integer userId, Integer cookbookId);

    /**
     * 根据食物id在食物库中查询食物
     * @param food_id
     * @return
     */
    @Select("select * from diet_food_category where id = #{food_id}")
    public DietFoodCategory findFoodById(Integer food_id);

    /**
     * 将用户的食谱详情保存至数据库中！！
     * @param userId
     * @param cookbookId
     * @param breakfastDetails
     * @param lunchDetails
     * @param dinnerDetails
     * @return
     */
    @Update("update user_diet_cookbook set breakfast_details = #{breakfastDetails}, lunch_details = #{lunchDetails}, dinner_details = #{dinnerDetails} where id = #{cookbookId} and user_id = 1")
    public int storeUserDietCookbookDetails(Integer userId, Integer cookbookId, String breakfastDetails, String lunchDetails, String dinnerDetails);
}
